package Tree;

import java.util.Deque;
import java.util.LinkedList;
import java.util.Queue;

/**
 * @BelongsProject: SeniorArchitect-LeetCode
 * @BelongsPackage: Tree
 * @Author: zhuangxiaoyan
 * @CreateTime: 2023-10-24  22:52
 * @Description: TODO
 * @Version: 1.0
 */
public class 二叉树对称101 {
    public boolean isSymmetric(TreeNode root) {
        if (root == null) {
            return true;
        }
        return checkisSymmetric(root, root);
    }

    // 检查两个二叉树是否相同
    public boolean checkisSymmetric(TreeNode p, TreeNode q) {
        if (p == null && q == null) {
            return true;
        }
        if (p == null || q == null) {
            return false;
        }
        return p.val == q.val && checkisSymmetric(p.left, q.right) && checkisSymmetric(p.right, q.left);
    }

    // 检查两个二叉树是否相同
    public boolean check(TreeNode left, TreeNode right) {
        if (left == null && right == null) {
            return true;
        }
        if (left == null || right == null) {
            return false;
        }
        if (left.val == right.val) {
            return true;
        }
        boolean left_res = check(left.left, right.left);
        boolean right_res = check(left.right, right.right);
        return left_res && right_res;
    }

    // 使用层序遍历的方法来实现检查两个树是否相等
    public boolean isSymmetric2(TreeNode root) {
        // 采用的双端队列
        Deque<TreeNode> deque = new LinkedList<>();
        deque.offerFirst(root.left);
        deque.offerLast(root.right);
        while (!deque.isEmpty()) {
            TreeNode leftNode = deque.pollFirst();
            TreeNode rightNode = deque.pollLast();
            if (leftNode == null && rightNode == null) {
                continue;
            }
            // 以上三个判断条件合并
            if (leftNode == null || rightNode == null || leftNode.val != rightNode.val) {
                return false;
            }
            deque.offerFirst(leftNode.left);
            deque.offerLast(rightNode.right);
            deque.offerFirst(leftNode.right);
            deque.offerLast(rightNode.left);
        }
        return true;
    }

    public boolean isSymmetric3(TreeNode root) {
        // 使用普通队列
        Queue<TreeNode> deque = new LinkedList<>();
        deque.offer(root.left);
        deque.offer(root.right);
        while (!deque.isEmpty()) {
            TreeNode leftNode = deque.poll();
            TreeNode rightNode = deque.poll();
            if (leftNode == null && rightNode == null) {
                continue;
            }
            // 以上三个判断条件合并
            if (leftNode == null || rightNode == null || leftNode.val != rightNode.val) {
                return false;
            }
            // 这里顺序与使用Deque不同
            deque.offer(leftNode.left);
            deque.offer(rightNode.right);
            deque.offer(leftNode.right);
            deque.offer(rightNode.left);
        }
        return true;
    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
